package com.xjrsoft.module.app.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.module.app.dto.SetModuleDto;
import com.xjrsoft.module.app.entity.XjrAppFunction;
import com.xjrsoft.module.app.entity.XjrAppMyFunction;
import com.xjrsoft.module.app.mapper.XjrAppFunctionMapper;
import com.xjrsoft.module.app.mapper.XjrAppMyFunctionMapper;
import com.xjrsoft.module.app.service.IXjrAppFunctionService;
import com.xjrsoft.module.app.service.IXjrAppMyFunctionService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class XjrAppMyFunctionImpl extends ServiceImpl<XjrAppMyFunctionMapper, XjrAppMyFunction> implements IXjrAppMyFunctionService {
    @Override
    public Boolean setModule(SetModuleDto dto) {


        remove(Wrappers.<XjrAppMyFunction>query().lambda().eq(XjrAppMyFunction::getUserId, SecureUtil.getUserId()));

        List<XjrAppMyFunction> myFunctions = new ArrayList<>();
        dto.getModuleIds().forEach(moduleid ->{
            XjrAppMyFunction myFunction = new XjrAppMyFunction();
            myFunction.setUserId(dto.getRoleid());
            myFunction.setFunctionId(moduleid);
            myFunctions.add(myFunction);
        });

        return saveBatch(myFunctions);
    }
}
